/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.sykj.modules.system.service.impl;

import com.sykj.modules.system.domain.Wallet;
import com.sykj.utils.ValidationUtil;
import com.sykj.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.sykj.modules.system.repository.WalletRepository;
import com.sykj.modules.system.service.WalletService;
import com.sykj.modules.system.service.dto.WalletDto;
import com.sykj.modules.system.service.dto.WalletQueryCriteria;
import com.sykj.modules.system.service.mapstruct.WalletMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.sykj.utils.PageUtil;
import com.sykj.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author czy
* @date 2021-06-21
**/
@Service
@RequiredArgsConstructor
public class WalletServiceImpl implements WalletService {

    private final WalletRepository walletRepository;
    private final WalletMapper walletMapper;

    @Override
    public Map<String,Object> queryAll(WalletQueryCriteria criteria, Pageable pageable){
        Page<Wallet> page = walletRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(walletMapper::toDto));
    }

    @Override
    public List<WalletDto> queryAll(WalletQueryCriteria criteria){
        return walletMapper.toDto(walletRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public WalletDto findById(Integer wId) {
        Wallet wallet = walletRepository.findById(wId).orElseGet(Wallet::new);
        ValidationUtil.isNull(wallet.getWId(),"Wallet","wId",wId);
        return walletMapper.toDto(wallet);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WalletDto create(Wallet resources) {
        return walletMapper.toDto(walletRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Wallet resources) {
        Wallet wallet = walletRepository.findById(resources.getWId()).orElseGet(Wallet::new);
        ValidationUtil.isNull( wallet.getWId(),"Wallet","id",resources.getWId());
        wallet.copy(resources);
        walletRepository.save(wallet);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer wId : ids) {
            walletRepository.deleteById(wId);
        }
    }

    @Override
    public void download(List<WalletDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (WalletDto wallet : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("余额", wallet.getBalance());
            map.put(" createTime",  wallet.getCreateTime());
            map.put(" updateTime",  wallet.getUpdateTime());
            map.put(" createBy",  wallet.getCreateBy());
            map.put(" updateBy",  wallet.getUpdateBy());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}